<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/8/24
 * Time: 10:25
 * 处理商品分类表的‘增删改查’的控制器
 */
class cateAction extends Action{
    const CACHE_FILE="cateReal.php";
    /*分类列表有上下级关系、有查询，不能分页*/
    public function cate_listOp(){
        /*查询：按分类名称，分类等级*/
        $field = isset($_POST['search_field'])?$_POST['search_field']:'';
        $value = isset($_POST['field_value'])?$_POST['field_value']:'';
       // echo $field;
        $cate_model= new cateModel();

        if($field!='' && $value!=''){
            $cate_list[0]= $cate_model->getThisCateAndChildren($field,$value);
        }else{
            $cache_obj = new Cache(self::CACHE_FILE);
            if($cache_obj->cacheFileExists()){
                $cate_list = $cache_obj->CacheRead();
            }else{
                //首次访问，缓存文件不存在，则读取数据库，同时将数据写入文件中
                $cate_list = $cate_model->getAllCateReal();
                $cache_obj->CacheWrite($cate_list);
                //die('filenot');
            }
        }
        foreach($cate_list as $k=>$v){
            switch($v['level']){
                case 0:
                    $cate_list[$k]['level_name'] = "顶级";
                    break;
                case 1:
                    $cate_list[$k]['level_name'] = "一级";
                    break;
                case 2:
                    $cate_list[$k]['level_name'] = "二级";
                    break;
                default:
                    $cate_list[$k]['level_name'] = "二级以上";
            }
        }
        include_once TEMPLATES_DIR.DS."cate_list.php";
    }

    /**
     * 分2步
     *分类添加要查询已有分类，添加分类名称时，要选择上级分类，为0表示顶级分类，同时不应与已有分类同名
     */
    public function cate_addOp(){
        if(isset($_POST['cate_submit'])){
           $cate_name = trim($_POST['cate_name']);
            $cate_parent_id = (int)$_POST['p_id'];
            //省略部分验证
            $cate_model = new cateModel();
            if($cate_model->checkCateName($cate_name)){
                $this->showMessage('分类名称重复','index.php?act=cate&op=cate_add');
            }

            if($cate_model->insertOneCate($cate_name,$cate_parent_id)){
                $cache_obj = new Cache(self::CACHE_FILE);

                $this->showMessage('添加分类成功','index.php?act=cate&op=cate_list',1);
            }else{
                $this->showMessage('添加分类失败','index.php?act=cate&op=cate_add');
            }
        }else{
            $cate_model= new cateModel();
            $cate_list = $cate_model->getAllCateReal();
            include_once TEMPLATES_DIR.DS."cate_add.php";
        }

    }

    /**
     *2步：1反显当前分类的属性信息，在修改分类上级时，不能选择自己及自己的子类2、编辑过程中，分类名称不能与其它分类名称重复
     */
    public function cate_editOp(){
        $cate_id = $_REQUEST['cate_id'];//包含了get,post,cookie

        if(isset($_POST['cate_edit'])){
            $cate_name = $_POST['cate_name'];
            $cate_parent_id = $_POST['p_id'];
            $cate_model= new cateModel();
            if($cate_model->checkCateName($cate_name,$cate_id)){
                $this->showMessage('分类名称重复','index.php?act=cate&op=cate_edit&cate_id='.$cate_id);
            }
            //查询所有分类中，自已及下级的id
            $cate_ids_array = $cate_model->cateSelfAndChildren($cate_id);
           // print_r($cate_ids_array) ;die;
            if(in_array($cate_parent_id,$cate_ids_array)){
                $this->showMessage('请重新选择上级分类','index.php?act=cate&op=cate_edit&cate_id='.$cate_id);
            }
            if($cate_model->updateCateById($cate_id,$cate_name,$cate_parent_id)){
                $this->showMessage('修改分类成功','index.php?act=cate&op=cate_list',1);
            }else{
                $this->showMessage('没作修改或修改失败','index.php?act=cate&op=cate_list');
            }
        }else{
            $cate_model= new cateModel();
            $cate_array = $cate_model->getCateById($cate_id);
            $cate_list = $cate_model->getAllCateReal();
            include_once TEMPLATES_DIR.DS."cate_eidt.php";
        }
    }

    /**
     *删除一个分类时，不能删除‘有下级’的分类
     */
    public function cate_del_oneOp(){
        $cate_id = $_GET['cate_id'];
        $cate_model = new cateModel();
        if(!$cate_model->is_leaf($cate_id)){
            $this->showMessage('该分类不能删除','index.php?act=cate&op=cate_list');
        }
        if($cate_model->deleteCateById($cate_id)){
            $this->showMessage('该分类删除成功','index.php?act=cate&op=cate_list',1);
        }else{
            echo "删除失败，系统原因";
        }
    }

    /*支持删除多个*/
    public function cate_del_moreOp(){
        $cate_ids = isset($_POST['cate_del'])?$_POST['cate_del']:0;
        $cate_model = new cateModel();
        if(empty($cate_ids)){
            $this->showMessage('请选择','index.php?act=cate&op=cate_list');
        }
        foreach($cate_ids as $cate_id){
            if(!$cate_model->is_leaf($cate_id)){
                $this->showMessage('请重新选择','index.php?act=cate&op=cate_list');
            }
        }
        if($cate_model->deleteCates($cate_ids)){
            $this->showMessage('多个分类删除成功','index.php?act=cate&op=cate_list',1);
        }else{
            echo "删除多个失败";
        }
    }

    /**
     *  列表数组返回为js的数组，每个元素为json对象并对应一条记录，数据为空数组则返回空数组
     */
    public function ajax_levelOp(){
        $cate_id = $_GET['cate_id'];
        $cate_model = new cateModel();
        $cate_next_level = $cate_model->getCateSons($cate_id);
        echo json_encode($cate_next_level);
    }
}